package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Like;
import cn.wolfcode.trip.base.domain.TInform;
import cn.wolfcode.trip.base.domain.TInformReadRecord;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.MessageQueryObject;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import java.util.*;

//文件上传Controller
@Controller
@RequestMapping("/messages")
public class MessageController {
    //自动注入容器,ServletContext
    @Autowired
    private ServletContext ctx;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IUserService userService;

    /**
     * 点赞功能
     *
     * @return
     */
    @PostMapping("/like")
    @ResponseBody
    public Object insertLike(Like like){
        JSONResult json = new JSONResult();
        try {
        messageService.insertLike(like);

        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }





    /**
     * 判断该是否被点赞
     */
    @GetMapping("/like")
    @ResponseBody
    public Boolean getLike(Like like){
       Like liked = messageService.checkLike(like);
        if (liked == null) {
            return true;
        }

        return false;
    }

    /**
     * 取消点赞
     * @param like
     * @return
     */
    @DeleteMapping("/like")
    @ResponseBody
    public Object deleteLike(Like like){
        JSONResult json = new JSONResult();
        try{
        messageService.deleteLike(like);

        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }

        return json;
    }






    /**
     查询所有系统消息
     系统消息为1
     * @param qo
     * @return
     */
    @GetMapping("/getSysMsg/{sysState}")
    @ResponseBody
    public Object getSysMsg(MessageQueryObject qo){
     PageInfo<TInform> inform= messageService.getSysMsg(qo);
        return inform;
    }

    /**
     * 获取私信消息
     * @param qo
     * @return
     */
    @GetMapping("/getPriMsg/{userId}/{informId}")
    @ResponseBody
    public Object getPriMsg(MessageQueryObject qo){
        qo.setSysState(0);
        List<TInform> priMsg = messageService.getPriMsg(qo);
        return priMsg;
    }

    /**
     * 获取用户总共未读条数
     * @param qo
     * @return
     */
    @GetMapping("/getPriMsgNum")
    @ResponseBody
    public Object getPriMsgNum(MessageQueryObject qo){
        //0为私信,系统消息在界面中,所以不做这样的了
        qo.setSysState(0);
        Integer num = messageService.getPriMsgNum(qo);
        return num;
    }




    /**
     * 获取发送私信给我的人的列表
     * 以及未读信息的数量
     * 在USER类里加了冗余字段
     * @param qo
     * @return
     */
    @GetMapping("/getPriMsgUser/{userId}")
    @ResponseBody
    public Object getPriMsgUser(MessageQueryObject qo){
        qo.setSysState(0);
        qo.setHasRead(0);
        //获取所有的已读未读人员信息
        List<TInformReadRecord> TInformReadRecords = messageService.getPriMsgUser(qo);
        //接收用户
        List<User> users= new ArrayList<>();
        List<String> temp = new ArrayList<>();
        for (TInformReadRecord tInformReadRecord : TInformReadRecords) {
            Long id = tInformReadRecord.getInform().getId();
            User user = userService.getUserById(id);
            qo.setInformId(id);
            int onePriMsgNum = messageService.getOnePriMsgNum(qo);
            String nickName = user.getNickName();
            if (!temp.contains(nickName)){
            user.setHasRead(onePriMsgNum);
                temp.add(nickName);
                users.add(user);
            }
        }

        return users;
    }

    /**
     * 更新已读状态
     * @param qo
     * @return
     */
    @PostMapping("/setHasRead")
    @ResponseBody
    public Object setHasRead(MessageQueryObject qo){
        JSONResult json = new JSONResult();
        try{
        //设置状态为已读
        TInformReadRecord tInformReadRecord = new TInformReadRecord();
        User inform = new User();
        inform.setId(qo.getInformId());
        tInformReadRecord.setInform(inform);
        tInformReadRecord.setUserId(qo.getUserId());
        tInformReadRecord.setHasRead(1);
        tInformReadRecord.setReadTime(new Date());
        messageService.setHasRead(tInformReadRecord);
        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }



    /**
     * 新增私信
     * @param
     * @return
     */
    @PostMapping("/insertPriMsg")
    @ResponseBody
    public Object insertPriMsg(Long informId,Long userId,String content){
        //设置新发送的私信
        TInform tInform = new TInform();
        tInform.setToAll(0);
        tInform.setCreatTime(new Date());
        tInform.setTitle("私信");
        tInform.setContent(content);
        messageService.insertInform(tInform);
        //设置私信到tif表上
        TInformReadRecord tInformReadRecord = new TInformReadRecord();
        User inform = new User();
        inform.setId(informId);
        tInformReadRecord.setInform(inform);
        tInformReadRecord.setUserId(userId);
        tInformReadRecord.setHasRead(0);
        tInformReadRecord.setId(tInform.getId());
        messageService.insetTif(tInformReadRecord);
        return new JSONResult();
    }

}
